using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.DBAccess;

public class Patient
{
    public static readonly string TABLE_NAME = "Patient";
    public static readonly string ID__COLUMN_NAME = "ID";
    public static readonly string CODE__COLUMN_NAME = "Code";
    public static readonly string PATIENTNAME__COLUMN_NAME = "PatientName";
    public static readonly string COMPANYID__COLUMN_NAME = "CompanyID";
    public static readonly string PASS__COLUMN_NAME = "Pass";
    public static readonly string FOLDERPATH__COLUMN_NAME = "FolderPath";
    public static readonly string CREATEDDATE__COLUMN_NAME = "CreatedDate";

    public int ID = 0;
    public string Code = string.Empty;
    public string PatientName = string.Empty;
    public int CompanyID = 0;
    public string Pass = string.Empty;
    public string FolderPath = string.Empty;
    public DateTime CreatedDate = DateTime.Now;

    public Patient()
    {
    }

    public Patient(int id)
    {
        WhereParameterCollection whereColl = new WhereParameterCollection();
        whereColl.Add(ID__COLUMN_NAME, id, CompareType.Equal);

        SortParameterCollection sortColl = new SortParameterCollection();
        sortColl.Add(ID__COLUMN_NAME, SortType.Increase);

        DBAccessor db = null;
        try
        {
            db = new DBAccessor();
            DataSet ds = db.GetTable(TABLE_NAME, whereColl, sortColl);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];

                this.FromDataRow(row);
            }
            else
            {
                this.ID = 0;
            }
        }
        catch (Exception)
        {
            ID = -1;
        }
        finally
        {
            if (db != null) { db.Dispose(); }
        }
    }

    public Patient(string code)
    {
        WhereParameterCollection whereColl = new WhereParameterCollection();
        whereColl.Add(CODE__COLUMN_NAME, code, CompareType.Like);

        SortParameterCollection sortColl = new SortParameterCollection();
        sortColl.Add(ID__COLUMN_NAME, SortType.Increase);

        DBAccessor db = null;
        try
        {
            db = new DBAccessor();
            DataSet ds = db.GetTable(TABLE_NAME, whereColl, sortColl);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];

                this.FromDataRow(row);
            }
            else
            {
                this.ID = 0;
            }
        }
        catch (Exception)
        {
            ID = -1;
        }
        finally
        {
            if (db != null) { db.Dispose(); }
        }
    }

    protected void FromDataRow(DataRow row)
    {
        this.ID = Convert.ToInt32(row[ID__COLUMN_NAME]);
        this.Code = row[CODE__COLUMN_NAME].ToString();
        this.PatientName = row[PATIENTNAME__COLUMN_NAME].ToString();
        this.CompanyID = Convert.ToInt32(row[COMPANYID__COLUMN_NAME]);
        this.Pass = row[PASS__COLUMN_NAME].ToString();
        this.FolderPath = row[FOLDERPATH__COLUMN_NAME].ToString();
        this.CreatedDate = DateTime.Parse(row[CREATEDDATE__COLUMN_NAME].ToString());
    }
}
